<?php
if (!class_exists("Bet_3d"))
{
    include_once( "class_3d/Bet_3d.php" );
}

class Bet_5h extends Bet_3s
{

    public function getMemBetList ($bettypeStr, $num, $page, $ids, $timeStart, $timeEnd, $lid = "", $where = "", $idsWhere = true)
    {
        $bettypeArr = explode("_", $bettypeStr);
        $bettype = $bettypeArr[0];
        $minNum = $this->gametype == 20 ? 5 : 8;
        if (in_array($bettype, $this->bet4subArr))
        {
            if (isset($bettypeArr[1]))
            {
                $subid = min($minNum, max(1, $bettypeArr[1]));
            }
            else
            {
                $subid = 1;
            }
        }
        else
        {
            $subid = 0;
        }
        $rs = new RecordSet();
        $pagesize = 20;
        $sql = "SELECT a.*, b.account,c.round, c.open FROM {$this->table_name} a";
        $sql .= " LEFT JOIN k_user b ON (a.user_id = b.id)";
        $sql .= " LEFT JOIN lotto_{$this->tbl} c ON (a.lotto_id = c.id)";
        $sql .= " WHERE a.bettype={$bettype} and a.subid={$subid} ";
        $num = trim($num);
        if (in_array($bettype, array(9, 10, 11, 12, 13, 14, 15, 16, 17)))
        {
            $sql .= " AND a.numbers like '%" . trim($num) . "%'";
        }
        else
        {
            $sql .= " AND a.numbers='" . trim($num) . "'";
        }
        if ($lid == "")
        {
            $sql .= " AND c.open BETWEEN '" . date("Y-m-d", $timeStart) . "' AND '" . date("Y-m-d", $timeEnd) . "'";
        }
        else
        {
            $sql .= " AND a.lotto_id ={$lid} ";
        }
        if ($ids === "")
        {
            $sql .= " AND 1 = 0 {$where}";
        }
        else
        {
            if ($idsWhere)
            {
                $sql .= " AND a.user_id IN ({$ids}) ";
            }
            $sql .= $where;
        }
        $sql .= " ORDER BY c.open DESC, a.createtime DESC, a.id DESC";
        $rs->open($sql, $page, $pagesize);
        return $rs;
    }

    public function getMemBetAllList ($bettypeStr, $page, $ids, $timeStart, $timeEnd, $lid = "", $where = "", $idsWhere = true)
    {
        $rs = new RecordSet();
        $bettypeArr = explode("_", $bettypeStr);
        $bettype = intval($bettypeArr[0]);
        if (in_array($bettype, $this->bet4subArr))
        {
            $minNum = $this->gametype == 20 ? 5 : 8;
            if (isset($bettypeArr[1]))
            {
                $subid = min($minNum, max(1, $bettypeArr[1]));
            }
            else
            {
                $subid = 1;
            }
        }
        else
        {
            $subid = 0;
        }
        $pagesize = 20;
        $sql = "SELECT a.*, b.account,c.round, c.open FROM {$this->table_name} a";
        $sql .= " LEFT JOIN k_user b ON (a.user_id = b.id)";
        $sql .= " LEFT JOIN lotto_{$this->tbl} c ON (a.lotto_id = c.id)";
        $sql .= " WHERE";
        if ($bettypeStr !== "")
        {
            $sql .= " bettype=" . $bettype . " AND subid = {$subid} AND ";
        }
        if ($lid == "")
        {
            $sql .= " c.open BETWEEN '" . date("Y-m-d", $timeStart) . "' AND '" . date("Y-m-d", $timeEnd) . "'";
        }
        else
        {
            $sql .= " a.lotto_id = {$lid}";
        }
        if ($ids === "")
        {
            $sql .= " AND 1 = 0 {$where}";
        }
        else
        {
            if ($idsWhere)
            {
                $sql .= " AND user_id IN ({$ids}) ";
            }
            $sql .= " " . $where;
        }
        $sql .= " ORDER BY a.createtime DESC, a.id DESC";
        $rs->open($sql, $page, $pagesize);
        return $rs;
    }

    public function getUserSumNumberAmount ($idstr, $bettypeStr, $number, $timeStart, $timeEnd, $lid = "", $where = "", $idsWhere = true)
    {
        $bettypeArr = explode("_", $bettypeStr);
        $bettype = $bettypeArr[0];
        if (isset($bettypeArr[1]))
        {
            $subid = $bettypeArr[1];
        }
        else
        {
            $subid = 0;
        }
        $reVal = false;
        $sqlLike = "";
        switch ($g_gametype_3d[$this->gametype]['type'])
        {
            case 3 :
                if ($this->gametype == 20)
                {
                    $gArr = array(9, 10, 11, 12, 13, 14);
                }
                else
                {
                    $gArr = array(10, 11, 12, 13, 14, 15, 16, 17);
                }
                break;
            default :
                $gArr = array(24, 25, 26, 27, 28, 30, 31, 32, 33, 34);
                break;
        }
        if ($bettype == "35")
        {
            $number1 = substr($number, 0, 1);
            $number2 = substr($number, 1, 1);
            $number3 = substr($number, 2, 1);
            $numberT = "%{$number1}%,%{$number2}%,%{$number3}%";
            $sqlLike = " AND numbers LIKE '{$numberT}'";
        }
        else if ($bettype == "46")
        {
            if ($number < 6)
            {
                $sqlLike = " AND numbers LIKE '{$number},%'";
            }
            else
            {
                if ($number < 12)
                {
                    $sqlLike = " AND numbers LIKE '%,{$number},%'";
                }
                else
                {
                    $sqlLike = " AND numbers LIKE '%,{$number}'";
                }
            }
        }
        else if (in_array($bettype, $gArr))
        {
            $numberArrTemp = explode(",", $number);
            if (count($numberArrTemp) == 1)
            {
                $sqlLike = " AND numbers LIKE '%{$number}%'";
            }
            else
            {
                $sqlLike = " AND numbers = '{$number}'";
            }
        }
        else
        {
            $sqlLike = " AND numbers = '{$number}'";
        }
        if ($number == "" || $number == "all")
        {
            $sqlLike = "";
        }
        $sql = "SELECT sum(a.amount) as amount, sum(a.b1) as b1, sum(a.a1) as a1  FROM " . $this->table_name;
        $sql .= " a LEFT JOIN lotto_{$this->tbl} b ON (a.lotto_id = b.id) ";
        if ($idstr === "")
        {
            $sql .= " WHERE 1 = 0 ";
        }
        else
        {
            $sql .= " WHERE ";
            if ($idsWhere)
            {
                $sql .= " user_id IN ({$idstr}) ";
            }
            else
            {
                $sql .= " 1 ";
            }
        }
        if ($bettypeStr !== "")
        {
            $sql .= " AND bettype = {$bettype} AND subid = {$subid} ";
        }
        $sql .= $sqlLike;
        if ($lid == "")
        {
            $sql .= " AND ( b.open BETWEEN '" . date("Y-m-d", $timeStart) . "' AND '" . date("Y-m-d", $timeEnd) . "')";
        }
        else
        {
            $sql .= " AND ( a.lotto_id = {$lid})";
        }
        if ($where == "")
        {
            $sql .= " AND a.rame IN (0,9)";
        }
        else
        {
            $sql .= " " . $where;
        }
        $result = $this->db->query($sql);
        if ($result)
        {
            return mysql_fetch_array($result, MYSQL_ASSOC);
        }
        return $reVal;
    }

    public function getMyBetAmount ($curruser, $bettype, $lotto_id, $numbers)
    {
        $theacl = $curruser['acl'];
        $bet_percent = "";
        $bet2_percent = "";
        switch ($theacl)
        {
            case 2 :
                $bet_percent = " * percent2 / 100";
                $bet2_percent = "";
                break;
            case 3 :
                $bet_percent = " * percent3 / 100";
                $bet2_percent = " * percent3 / 100";
                break;
            case 4 :
                $bet_percent = " * percent4 / 100";
                $bet2_percent = " * percent4 / 100";
                break;
            case 5 :
                $bet_percent = " * percent5 / 100";
                $bet2_percent = " * percent5 / 100";
                break;
            default :
                exit("error acl for bnow_do_5h");
                exit();
        }
        $betStr = " AND bettype = {$bettype} ";
        $sql = "select sum(amount {$bet_percent}) as amount, numbers from bet_{$this->tbl} where lotto_id={$lotto_id} and rame in (0,9) {$betStr} and numbers = '{$numbers}'";
        $sql2 = "select sum(amount {$bet2_percent}) as amount, numbers from bet2_{$this->tbl} where lotto_id={$lotto_id} and rame in (0,9) {$betStr} and numbers = '{$numbers}'";
        $sql3 = "select sum(amount * -1) as amount, numbers from bet2_{$this->tbl} where lotto_id={$lotto_id} and rame in (0,9) {$betStr} and numbers = '{$numbers}'";
        $userlist = GetUserList($curruser['id']);
        if ($userlist == "")
        {
            $sql .= " and 0=1 ";
        }
        else
        {
            $sql .= " and user_id in ({$userlist})";
        }
        if (2 < $theacl)
        {
            $sql2 .= " and uperid{$theacl}=" . $curruser['id'];
        }
        $sql3 .= " and user_id=" . $curruser['id'];
        $sql .= " GROUP BY numbers ";
        $sql2 .= " GROUP BY numbers ";
        $sql3 .= " GROUP BY numbers ";
        $sqlOK = "select sum(amount) from (";
        $sqlOK .= " {$sql} UNION ALL {$sql2} UNION ALL {$sql3}) as tblA WHERE tblA.numbers = '{$numbers}' GROUP BY tblA.numbers";
        $sqlOKRS = getdb($sqlOK);
        $sqlOKArr = array();
        $totalAmount = 0;
        while (!$sqlOKRS->eof)
        {
            $totalAmount += $sqlOKRS->f[0];
            $sqlOKRS->next();
        }
        return intval($totalAmount);
    }

    public function dbInsert ($fields)
    {
        $sql = "INSERT INTO {$this->table_name} (";
        $sql .= implode(",", array_keys($fields));
        $sql .= ") VALUES (";
        $sql .= implode(",", array_values($fields));
        $sql .= ")";
        if (!$this->db->query($sql))
        {
            return false;
        }
        $result=mysql_insert_id($this->db->handle_master);
		//扣除额度
		$this->db->query('update k_user set balance=balance-'.$fields['amount'].' where id='.$fields['user_id']);
        return $result;
    }

    public function getMyMemberIdByAcl ($id, $acl)
    {
        $reVal = "";
        switch ($acl)
        {
            case "255" :
                $sql = "SELECT id FROM k_user WHERE acl = '1'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $reVal = implode(",", $idArr);
                break;
            case "5" :
                $sql = "SELECT id FROM k_user WHERE acl = '4' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id4str = implode(",", $idArr);
                if ($id4str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '3' AND prev_id IN(" . $id4str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id3str = implode(",", $idArr);
                if ($id3str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '2' AND prev_id IN (" . $id3str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id2str = implode(",", $idArr);
                if ($id2str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id IN (" . $id2str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
                break;
            case "4" :
                $sql = "SELECT id FROM k_user WHERE acl = '3' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id3str = implode(",", $idArr);
                if ($id3str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '2' AND prev_id IN (" . $id3str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id2str = implode(",", $idArr);
                if ($id2str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id IN (" . $id2str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
                break;
            case "3" :
                $sql = "SELECT id FROM k_user WHERE acl = '2' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id2str = implode(",", $idArr);
                if ($id2str == "")
                {
                    return "";
                }
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id IN (" . $id2str . ")";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
                break;
            case "2" :
                $sql = "SELECT id FROM k_user WHERE acl = '1' AND prev_id = '" . $id . "'";
                $idArr = array();
                $result = $this->db->query($sql);
                while ($row = mysql_fetch_array($result))
                {
                    $idArr[] = $row[0];
                }
                $id1str = implode(",", $idArr);
                $reVal = $id1str;
                break;
        }
        return $reVal;
    }

}

?>